/*
 * Project: Made 2 Order Software utility package
 * Package: com.made2ordersoftware.util
 * File:    DateUtility.java
 * Created: Dec 25, 2010
 */

/*
 *  Copyright (c) 2010 Made 2 Order Software, LLC
 *
 *  All rights reserved. No part of this software may be reproduced, stored in a retrieval system, or
 *  transmitted in any form or by any means, without the prior written permission of Made 2 Order Software.
 *
 *  You may not distribute the URL or redistribute the files to third parties. You may transfer the files
 *  onto a single storage device so long as you can prove ownership of the software. You may not reverse
 *  engineer, decompile, or disassemble the software or media files. You may not modify or translate the
 *  software or media, or distribute copies of the software or media without the written consent of Made
 *  2 Order Software
 */
package com.made2ordersoftware.util;

import java.util.Calendar;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Class containing public static and helper methods for Date functions.
 *
 * @author David Canning <dcannin@gmail.com>
 * @version 1.0.0
 */
public class DateUtility {

    private static final Logger LOG = LoggerFactory.getLogger(DateUtility.class);

    /**
     * Calculates the number of days between 2 dates. This calculation returns a float and requires the user
     * to determine precision.
     *
     * @param begin The date we want to begin the calculations with.
     * @param end   The date we want to end the calculations with.
     *
     * @return The float representing the number of days between begin and end.
     */
    public static long daysBetween2Dates(Date begin, Date end) {
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(begin);
        c2.setTime(end);
        return ((c2.getTime().getTime() - c1.getTime().getTime()) / (24 * 3600 * 1000));
    }

    /**
     * Calculates the age given by brithDate and the current system date. This calculation is rounded down to
     * represent a "real" age.
     *
     * @param birthDate The birth date to be used for calculations
     *
     * @return The age
     */
    public static int calculateAge(Date birthDate) {
        LOG.info("Calculating age");
        return (int) daysBetween2Dates(birthDate, new Date()) / 365;
    }

}
